import 'package:color_dart/color_dart.dart';
import "package:flutter/material.dart";
import 'package:im_flutter_sdk_example/components/a_row/a_row.dart';
import 'package:im_flutter_sdk_example/provider/user_model.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
import 'package:im_flutter_sdk_example/pages/chat_page.dart';
import 'package:provider/provider.dart';

class HeadItem extends StatelessWidget {
  final String title;
  final String count;
  final Function press;

  const HeadItem({Key key, this.title, this.count, this.press})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: press,
      child: Column(children: [
        Padding(
          padding: const EdgeInsets.only(top: 10, bottom: 4),
          child: Text(count,
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 21,
                  fontWeight: FontWeight.w500)),
        ),
        Text(title,
            style: TextStyle(
                color: rgba(255, 255, 255, 1),
                fontSize: 12,
                fontWeight: FontWeight.bold)),
      ]),
    );
  }
}

class HeadSectionOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 0),
      child: _buildSectionOne(context),
    );
  }

  Widget _buildDivider() {
    return SizedBox(
        height: 24,
        width: 1,
        child: Container(
          color: rgba(213, 219, 230, 0.6),
        ));
  }

  Container _buildSectionOne(BuildContext context) {
    UserProvider userProvider = Provider.of<UserProvider>(context);

    return Container(
      height: 84,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          HeadItem(
              title: "关注",
              count: '${userProvider.focusCount}',
              press: () => {G.pushNamed('/attention')}),
          _buildDivider(),
          HeadItem(
              title: "收藏",
              count: '${userProvider.collectCount}',
              press: () => {G.pushNamed('/collection')}),
          _buildDivider(),
          HeadItem(
              title: "足迹",
              count: '${userProvider.footprintCount}',
              press: () => {G.pushNamed('/footprint')})
        ],
      ),
    );
  }
}

class HeadSectionThree extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return _buildSectionOne(context);
  }

  void _chatToService(context) async {
    String chatter = "34"; // 约定 34 客服人员
    EMConversationType type = EMConversationType.Chat;
    try {
      EMConversation conversation =
          await EMClient.getInstance.chatManager.getConversation(chatter, type);
      // conversation
      conversation.ext = {"nickname": "在线客服"};
      Navigator.push(context,
          new MaterialPageRoute(builder: (BuildContext context) {
        return new ChatPage(conversation: conversation);
      }));
    } catch (e) {
      print('create conversation error');
    }
  }

  Widget _buildSectionOne(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          ARow(
            height: 60,
            leftChild: Container(
              width: 30,
              child: Image.asset('lib/assets/images/mine/label.png',
                  width: 16, height: 18, fit: BoxFit.contain),
            ),
            centerChild: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                '我的标签',
                style: TextStyle(color: rgba(56, 56, 56, 1)),
              ),
            ),
            onPressed: () => {G.pushNamed('/select_tags')},
            rightChild: Row(
              children: <Widget>[
                icontupian6(color: rgba(197, 196, 196, 1), size: 14)
              ],
            ),
          ),
          ARow(
            height: 60,
            leftChild: Container(
              width: 30,
              child: Image.asset('lib/assets/images/mine/account.png',
                  width: 18, height: 18, fit: BoxFit.contain),
            ),
            centerChild: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                '账户与安全',
                style: TextStyle(color: rgba(56, 56, 56, 1)),
              ),
            ),
            onPressed: () => {G.pushNamed('/account_security')},
            rightChild: Row(
              children: <Widget>[
                icontupian6(color: rgba(197, 196, 196, 1), size: 14)
              ],
            ),
          ),
          ARow(
            height: 60,
            leftChild: Container(
              width: 30,
              child: Image.asset('lib/assets/images/mine/contact.png',
                  width: 19, height: 16, fit: BoxFit.contain),
            ),
            centerChild: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                '在线客服',
                style: TextStyle(color: rgba(56, 56, 56, 1)),
              ),
            ),
            onPressed: () => _chatToService(context),
            rightChild: Row(
              children: <Widget>[
                icontupian6(color: rgba(197, 196, 196, 1), size: 14)
              ],
            ),
          ),
          ARow(
              height: 60,
              leftChild: Container(
                width: 30,
                child: Image.asset('lib/assets/images/mine/about.png',
                    width: 20, height: 18, fit: BoxFit.contain),
              ),
              centerChild: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  '关于我们',
                  style: TextStyle(color: rgba(56, 56, 56, 1)),
                ),
              ),
              rightChild: Row(
                children: <Widget>[
                  icontupian6(color: rgba(197, 196, 196, 1), size: 14)
                ],
              ),
              onPressed: () => {G.pushNamed('/about')})
        ],
      ),
    );
  }
}

class HeadSectionFour extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      width: 360,
      decoration: BoxDecoration(
          color: Colors.white, //和Container下面的color会冲突 具体原因不详
          border: new Border.all(
            //添加边框
            width: 10.0, //边框宽度
            color: Colors.white10, //边框颜色
          ),
          borderRadius: new BorderRadius.all(Radius.circular(20.0)), //设置圆角
          boxShadow: <BoxShadow>[
            //
          ]),
      child: Center(
        child: Text("退出账号",
            textAlign: TextAlign.center,
            style: TextStyle(color: Colors.black87)),
      ),
    );
  }
}
